package org.javaboy.vhr.mapper;

import org.apache.ibatis.annotations.*;
import org.javaboy.vhr.model.Car;
import org.javaboy.vhr.model.CarManager;

import java.util.Date;
import java.util.List;

/**
 * ClassName: CarinfoMapper <br/>
 * Description: <br/>
 * date: 2020/2/29 8:02<br/>
 *
 * @author Hesion<br />
 * @since JDK 1.8
 */
@Mapper
public interface CarinfoMapper {
    @Select({"<script> " +
            "SELECT * FROM car  WHERE del_flag = '0'" +
            "<if test=\"car.model != null and car.model != ''\">" +
            "AND model like concat('%',#{car.model},'%')</if>" +
            "<if test=\"car.insstatu != null and car.insstatu != ''\">" +
            "AND insstatu like concat('%',#{car.insstatu},'%')</if>" +
            "<if test=\"car.yearstatu != null and car.yearstatu != ''\">" +
            "AND yearstatu like concat('%',#{car.yearstatu},'%')</if>" +
            "<if test=\"car.mileage != null and car.mileage != ''\">" +
            "AND mileage = #{car.mileage}</if>" +
            "<if test=\"car.spend != null and car.spend != ''\">" +
            "AND spend = #{car.spend}</if>" +
            "<if test=\"page != null and size != null\">" +
            "limit #{page},#{size}</if>" +
            "</script>"})
    @Results(id = "CarinfoResult",
            value = {
                    @Result(property = "createTime", column = "create_time"),
                    @Result(property = "updateTime", column = "update_time"),
                    @Result(property = "createBy", column = "createby"),
                    @Result(property = "updateBy", column = "updateby"),
            })
    List<Car> getCarByPage(Integer page, Integer size, Car car, Date[] beginDateScope);


    @Delete("DELETE FROM car WHERE id = #{id}")
    int deleteCarByEid(String id);

    @Insert("INSERT INTO `car` VALUES(#{id},#{model},#{insstatu},#{yearstatu},#{mileage},#{spend},#{borrow},#{createTime},#{createBy},#{updateTime},#{updateBy},\"0\")")
    int addCar(Car car);

    @Update({"<script> " +
            "UPDATE  car " +
            "<set> " +
            "<if test=\"model != null\">" +
            "model=#{model},</if>" +
            "<if test=\"insstatu != null\">" +
            "insstatu=#{insstatu},</if>" +
            "<if test=\"yearstatu != null\">" +
            "yearstatu=#{yearstatu},</if>" +
            "<if test=\"mileage != null\">" +
            "mileage=#{mileage},</if>" +
            "<if test=\"spend != null\">" +
            "spend=#{spend},</if>" +
            "<if test=\"borrow != null\">" +
            "borrow=#{borrow},</if>" +
            "<if test=\"updateTime != null\">" +
            "update_time=#{updateTime},</if>" +
            "<if test=\"updateBy != null\">" +
            "updateby=#{updateBy},</if>" +
            "</set>" +
            "WHERE id=#{id}" +
            "</script>"})
    int updateByPrimaryKeySelective(Car car);

    @Select({"<script> " +
            "SELECT count(*) FROM car  where del_flag = '0'" +
            "<if test=\"car.model != null\">" +
            "and model=#{car.model}</if>" +
            "<if test=\"car.insstatu != null\">" +
            "and insstatu=#{car.insstatu}</if>" +
            "<if test=\"car.yearstatu != null\">" +
            "and yearstatu=#{car.yearstatu}</if>" +
            "<if test=\"car.mileage != null\">" +
            "and mileage=#{car.mileage}</if>" +
            "</script>"})
    Long getTotal(Car car, Date[] beginDateScope);

    @Insert("INSERT INTO car_manager VALUES(#{id},#{cId},#{mId},#{createTime},#{createBy},#{updateTime},#{updateBy},#{del_flag})")
    int mallocM(CarManager carManager);
}
